package com.sample.rx.base;

import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

public abstract class BasicActivity extends AppCompatActivity implements IFrag2ActImpl {
    protected abstract
    @LayoutRes
    int actLayoutRes();

    public abstract void afterOnCreate();

    public abstract void bindViews();

    private ActionBar actionBar;

    public boolean isFullScreen() {
        return false;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(actLayoutRes());
        bindViews();
        afterOnCreate();
    }

    public void hideActionBar() {
        if (null != getActionBar())
            getActionBar().hide();
        if (null != getSupportActionBar())
            getSupportActionBar().hide();
    }

    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        if (isFullScreen()) {
            hideActionBar();
        }
    }

    @Override
    public void setSupportActionBar(@Nullable Toolbar toolbar) {
        super.setSupportActionBar(toolbar);
        actionBar = getSupportActionBar();
        setActionBack(true);
    }

    public void setActionBack(boolean showHomeAsUp) {
        if (null != actionBar) {
            actionBar.setDisplayHomeAsUpEnabled(showHomeAsUp);
            actionBar.setHomeButtonEnabled(showHomeAsUp);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onFrag2Act(String tag, Object... objectParams) {
       /* if (TextUtils.equals("show", tag)) {
            Object object = objectParam[0];
            if (object instanceof CharSequence) {
                show((CharSequence) object);
            } else if (object instanceof Integer) {
                show((Integer) object);
            }
        }*/
    }
}
